#include <cmath>

#include <algorithm>

#include <co/dataIStream.h>
#include <co/dataOStream.h>

#include "objstate.h"

eqtest::ObjState::ObjState()
{
    light.x = -1.62f;
    light.y = 0.80f;
    light.z = -3.2f;
    rotate.x = rotate.y = 0.0f;
}

static inline float bound(float x, float lower = 0.0f, float upper = 1.0f)
{
    return std::min(std::max(x, lower), upper);
}

void eqtest::ObjState::addRotate(float drx, float dry)
{
    rotate.x += drx;
    rotate.y += dry;
    rotate.x = std::fmod(rotate.x, 360.0f);
    rotate.y = std::fmod(rotate.y, 360.0f);
}

void eqtest::ObjState::getInstanceData(co::DataOStream& os)
{
    os << light.x << light.y << light.z;
    os << rotate.x << rotate.y;
}

void eqtest::ObjState::applyInstanceData(co::DataIStream& is)
{
    is >> light.x >> light.y >> light.z;
    is >> rotate.x >> rotate.y;
}
